package rs.pnv.model.insurance;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotBlank;

import rs.pnv.model.AbstractEntity;

@Entity
@Table(name = "SEP_VEHICLE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Vehicle extends AbstractEntity {

	public enum VehicleType {
		CAR,
		TRUCK,
		BOAT
	}

	@NotBlank
	@Column(nullable = false)
	@XmlElement(nillable = false)
	private String model;

	@NotBlank
	@Column(nullable = false)
	@XmlElement(nillable = false)
	private VehicleType type;

	@NotBlank
	@Column(nullable = false)
	@XmlElement(nillable = false)
	private Integer yearOfProduction;

	@NotBlank
	@Column(nullable = false, unique = true)
	@XmlElement(nillable = false)
	private String registrationPlateNumber;

	@NotBlank
	@Column(nullable = false, unique = true)
	@XmlElement(nillable = false)
	private String chassisNumber; // broj šasije

	@NotBlank
	@OneToOne
	@JoinColumn(nullable = false)
	@XmlElement(nillable = false)
	private Owner owner;

	@OneToOne(optional = true)
	private TravelHelpInsurance travelHelpInsurance;

	public Vehicle() {

	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public VehicleType getType() {
		return type;
	}

	public void setType(VehicleType type) {
		this.type = type;
	}

	public Integer getYearOfProduction() {
		return yearOfProduction;
	}

	public void setYearOfProduction(Integer yearOfProduction) {
		this.yearOfProduction = yearOfProduction;
	}

	public String getRegistrationPlateNumber() {
		return registrationPlateNumber;
	}

	public void setRegistrationPlateNumber(String registrationPlateNumber) {
		this.registrationPlateNumber = registrationPlateNumber;
	}

	public String getChassisNumber() {
		return chassisNumber;
	}

	public void setChassisNumber(String chassisNumber) {
		this.chassisNumber = chassisNumber;
	}

	public Owner getOwner() {
		return owner;
	}

	public void setOwner(Owner owner) {
		this.owner = owner;
	}

	public TravelHelpInsurance getTravelHelpInsurance() {
		return travelHelpInsurance;
	}

	public void setTravelHelpInsurance(TravelHelpInsurance travelHelpInsurance) {
		this.travelHelpInsurance = travelHelpInsurance;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
				.append(model)
				.append(type)
				.append(yearOfProduction)
				.append(registrationPlateNumber)
				.append(chassisNumber)
				.append(owner)
				.appendSuper(super.hashCode())
				.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}

		final Vehicle otherVehicle = (Vehicle) obj;

		return new EqualsBuilder()
				.append(model, otherVehicle.getModel())
				.append(type, otherVehicle.getType())
				.append(yearOfProduction, otherVehicle.getYearOfProduction())
				.append(registrationPlateNumber, otherVehicle.getRegistrationPlateNumber())
				.append(chassisNumber, otherVehicle.getChassisNumber())
				.append(owner, otherVehicle.getOwner())
				.appendSuper(super.equals(obj))
				.isEquals();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("model", model)
				.append("type", type)
				.append("yearOfProduction", yearOfProduction)
				.append("registrationPlateNumber", registrationPlateNumber)
				.append("chassisNumber", chassisNumber)
				.append("owner", owner)
				.append(super.toString())
				.toString();
	}

}
